<template>
  <div class="subject-content">

    <!-- 工具栏   -->
    <el-row :gutter="10" style="margin-bottom: 15px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
      </el-col>
    </el-row>

    <!-- 表格     -->
    <el-table v-if="refreshTable" v-loading="loading" :data="menuList" row-key="menuId" :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="菜单名称" prop="menuName" width="200" />
      <el-table-column label="图标" width="150" align="center"><template v-slot="scope"><i :class="scope.row.icon"/></template></el-table-column>
      <el-table-column label="排序" prop="sort" width="150" sortable />
      <el-table-column label="权限标识" prop="perms" align="center"/>
      <el-table-column label="路由地址" prop="path" align="center"/>
      <el-table-column label="更新时间" prop="updateTime" align="center" sortable />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="rowUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.menuType != 'C'" size="mini" type="text" icon="el-icon-plus" @click="rowAdd(scope.row)">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="rowDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  添加、修改  -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="menuform" :model="menuform" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单">
              <Treeselect
                  v-model="menuform.parentId"
                  :options="menuOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="menuType">
              <el-radio-group v-model="menuform.menuType">
                <el-radio label="A">目录</el-radio>
                <el-radio label="B">菜单</el-radio>
                <el-radio label="C">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="menuform.menuType != 'C'">
            <el-form-item label="菜单图标" prop="icon">
              <el-input v-model="menuform.icon" placeholder="请输入图标"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="menuform.menuName" placeholder="请输入菜单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sort">
              <el-input-number v-model="menuform.sort" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="menuform.menuType != 'C'">
            <el-form-item label="打开方式" prop="target">
              <el-radio-group v-model="menuform.target">
                <el-radio label="tab">窗口</el-radio>
                <el-radio label="new">外链</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="menuform.menuType != 'C'">
            <el-form-item label="路由地址" prop="path">
              <el-input v-model="menuform.path" placeholder="请输入路由地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="menuform.menuType != 'A'">
            <el-form-item label="权限标识" prop="perms">
              <el-input v-model="menuform.perms" placeholder="请输入权限标识"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="menuform.menuType != 'C'">
            <el-form-item prop="visible">
              <span slot="label">
                <el-tooltip content="选择隐藏则不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                显示状态
              </span>
              <el-radio-group v-model="menuform.visible">
                <el-radio label="1">显示</el-radio>
                <el-radio label="0">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input type="textarea" v-model="menuform.remarks" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addMenu, deleteMenu, getMenuById, queryMenuList, updateMenu} from "@/api/admin/menu";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: "Menu",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 菜单表单
      menuform: {},
      // 菜单树选项
      menuOptions: [],
      // 表单校验
      rules: {
        menuName: [
          {required: true, message: "菜单名称不能为空", trigger: "blur"}
        ],
      }
    }
  },

  methods: {

    // 获取菜单列表
    queryMenuList() {
      queryMenuList().then(res => {
        this.menuList = this.handleTree(res.data, "menuId")
        this.loading = false;
      })
    },

    // 表单重置
    reset() {
      this.menuform = {
        menuId: undefined,
        parentId: undefined,
        menuName: undefined,
        menuType: "A",
        icon: undefined,
        sort: 0,
        target: "tab",
        path: undefined,
        perms: undefined,
        visible: "1",
        remarks: undefined,
      };
      this.resetForm("menuform");
    },

    // 头工具--添加
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加菜单";
    },

    // 提交按钮
    submitForm() {
      this.$refs.menuform.validate(valid => {
        if (valid) { // 修改
          if (this.menuform.menuId != undefined) {
            updateMenu(this.menuform).then(res => {
              this.$modal.msgSuccess(res.msg);
              this.open = false;
              this.queryMenuList()
            })
          } else {// 添加
            if (this.menuform.menuType == "A") {// 如果 menuType == "A" 就是目录   perms  就应该为空
              this.menuform.perms = undefined;
            } else if (this.menuform.menuType == "C") { // 如果 menuType == "C" 就是目录   path  icon   target visible  就应该为空
              this.menuform.path = undefined;
              this.menuform.icon = undefined;
              this.menuform.target = undefined;
              this.menuform.visible = undefined;
            }
            addMenu(this.menuform).then(res => {
              this.$modal.msgSuccess(res.msg);
              this.open = false;
              this.queryMenuList()
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 行工具---修改
    rowUpdate(row) {
      this.reset();
      this.getTreeselect();
      getMenuById(row.menuId).then(res => {
        this.menuform = res.data;
        if (res.data.parentId == 0) {
          this.menuform.parentId = undefined;
        }
        this.open = true;
        this.title = "修改菜单";
      })
    },

    // 行工具--添加
    rowAdd(row) {
      this.reset();
      this.menuform.parentId = row.menuId;
      this.getTreeselect();
      if (row.menuType == "A") {
        this.menuform.menuType = "B"
      } else if (row.menuType == "B") {
        this.menuform.menuType = "C"
        this.menuform.target = undefined
      }
      this.open = true;
      this.title = "添加菜单";
    },

    // 行工具--删除
    rowDelete(row) {
      deleteMenu(row.menuId).then(res => {
        this.queryMenuList();
        this.$modal.msgSuccess(res.msg);
      })
    },

    // 查询菜单下拉树结构
    getTreeselect() {
      queryMenuList().then(res => {
        this.menuOptions = [];
        this.menuOptions = this.handleTree(res.data, "menuId");
      })
    },

    // 转换菜单数据结构
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
  },
  created() { // 初始化
    this.queryMenuList()
  }
}
</script>

<style scoped>

</style>
